package pattern.observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 被观察者
 */
public class ProductList extends Observable {
    private List<String> productList = null;
    private static ProductList instance;

    private ProductList() {

    }

    public static ProductList getInstance() {
        if (null == instance) {
            instance = new ProductList();
            instance.productList = new ArrayList<>();
        }
        return instance;
    }

    /**
     * 添加观察者
     *
     * @param observer 观察者
     */
    public void addProductListObserver(Observer observer) {
        this.addObserver(observer);
    }

    public void addProduct(String newProduct) {
        productList.add(newProduct);
        System.out.println("产品列表添加了新的商品: " + newProduct);
        this.setChanged();
        this.notifyObservers(newProduct);
    }
}
